﻿// -------------------------------------------------------------------------
//    @FileName         :    NFSnsLoginModule.cpp
//    @Author           :    gaoyi
//    @Date             :    2025-03-29
//    @Email			:    445267987@qq.com
//    @Module           :    NFSnsLoginModule
//
// -------------------------------------------------------------------------

#include "NFSnsLoginModule.h"

#include <NFSnsOnlineRole.h>
#include <NFSnsOnlineRoleService.h>
#include <NFSnsEventDisp.h>
#include <NFSnsRoleDetail.h>
#include <NFSnsRoleDetailService.h>
#include <NFTransRoleOnline.h>

#include "NFServerComm/NFServerCommon/NFIServerMessageModule.h"

NFSnsLoginModule::NFSnsLoginModule(NFIPluginManager* p) : NFMMODynamicModule(p)
{
}

NFSnsLoginModule::~NFSnsLoginModule()
{
}

int NFSnsLoginModule::Awake()
{
    RegisterServerMessage(NF_ST_SNS_SERVER, proto_ff::E_GS_LOGIN_REQ);
    RegisterServerMessage(NF_ST_SNS_SERVER, proto_ff::E_GS_LOGOUT_REQ);
    return 0;
}

int NFSnsLoginModule::Tick()
{
    return 0;
}

int NFSnsLoginModule::OnDynamicPlugin()
{
    return NFIModule::OnDynamicPlugin();
}

int NFSnsLoginModule::OnExecute(uint32_t serverType, uint32_t nEventID, uint32_t bySrcType, uint64_t nSrcID, const google::protobuf::Message* pMessage)
{
    return 0;
}

int NFSnsLoginModule::OnHandleClientMessage(uint64_t unLinkId, NFDataPackage& packet)
{
    if (!m_pObjPluginManager->IsInited(NF_ST_SNS_SERVER))
    {
        NFLogError(NF_LOG_DEFAULT, packet.nParam1, "Center Server not inited, drop client msg:{}", packet.ToString());
        return -1;
    }

    if (m_pObjPluginManager->IsServerStopping())
    {
        NFLogError(NF_LOG_DEFAULT, packet.nParam1, "Center Server is Stopping, drop client msg:{}", packet.ToString());
        return -1;
    }


    switch (packet.nMsgId)
    {
        default:
        {
            NFLogError(NF_LOG_DEFAULT, 0, "Client MsgId:{} Register, But Not Handle, Package:{}", packet.nMsgId, packet.ToString());
            break;
        }
    }
    return 0;
}

int NFSnsLoginModule::OnHandleServerMessage(uint64_t unLinkId, NFDataPackage& packet)
{
    if (!m_pObjPluginManager->IsInited(NF_ST_SNS_SERVER))
    {
        NFLogError(NF_LOG_DEFAULT, packet.nParam1, "Sns Server not inited, drop client msg:{}", packet.ToString());
        return -1;
    }

    if (m_pObjPluginManager->IsServerStopping())
    {
        NFLogError(NF_LOG_DEFAULT, packet.nParam1, "Sns Server is Stopping, drop client msg:{}", packet.ToString());
        return -1;
    }

    int iRetCode = 0;
    switch (packet.nMsgId)
    {
        case proto_ff::E_GS_LOGIN_REQ:
        {
            iRetCode = OnHandleLoginReq(unLinkId, packet);
            break;
        }
        case proto_ff::E_GS_LOGOUT_REQ:
        {
            iRetCode = OnHandleLogoutReq(unLinkId, packet);
            break;
        }
        default:
        {
            NFLogError(NF_LOG_DEFAULT, 0, "Server MsgId:{} Register, But Not Handle, Package:{}", packet.nMsgId, packet.ToString());
            break;
        }
    }
    return iRetCode;
}

int NFSnsLoginModule::OnHandleLoginReq(uint64_t unLinkId, NFDataPackage& packet)
{
    proto_ff::Proto_GSLoginReq req;
    CLIENT_MSG_PROCESS_WITH_PRINTF(packet, req);

    NFTransRoleOnline* pTrans = NFTransRoleOnline::CreateTrans();
    CHECK_EXPR(pTrans, -1, " Create Trans sns role online Failed!UsedItem:{}", NFTransRoleOnline::GetStaticUsedCount());
    LOGSVR_TRACE("role online simple: " << req.role_id());
    int iRetCode = pTrans->RoleLogin(&req);
    CHECK_ERR_AND_FIN_TRANS(iRetCode, pTrans, "RoleLogin Failed!");
    return 0;
}

int NFSnsLoginModule::OnHandleLogoutReq(uint64_t unLinkId, NFDataPackage& packet)
{
    proto_ff::Proto_GSLogoutReq req;
    CLIENT_MSG_PROCESS_WITH_PRINTF(packet, req);

    proto_ff::Proto_SGLogoutRsp rsp;
    rsp.set_role_id(req.role_id());
    rsp.set_gamesvr_id(req.gamesvr_id());
    rsp.set_result(0);

    uint64_t roleId = req.role_id();
    auto pRoleDetail = NFSnsRoleDetailService::GetRoleDetail(roleId);
    CHECK_NULL_WF(roleId, pRoleDetail, "Sns Logout Failed, Can't find the roleId");

    NFSnsOnlineRole* pOnlineRole = NFSnsOnlineRoleService::GetOnlineRole(roleId);
    if (pOnlineRole)
    {
        LOG_ERR_IF(pOnlineRole->GetLogicSvrId() != req.gamesvr_id(), roleId, -1, "role online logic svr id:{} is not logout req 's game id:{}", pOnlineRole->GetLogicSvrId(), req.gamesvr_id());
        NFSnsOnlineRoleService::DelOnlineRole(roleId);
    }

    NFSnsRoleDetailService::RemoveRoleDetail(roleId);

    FindModule<NFIServerMessageModule>()->SendMsgToLogicServer(NF_ST_SNS_SERVER, req.gamesvr_id(), proto_ff::E_SG_LOGOUT_RSP, rsp,  roleId);

    LOG_INFO(roleId, "Sns Logout Success!");
    return 0;
}
